package com.express.common;

import com.express.constants.Constants;
import com.express.entity.Parameter;
import com.express.service.TokenService;
import com.express.sys.mapper.SysParameterMapper;
import com.express.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

@Component
public class SysParamterUtil {

    private static final Logger log = LoggerFactory.getLogger(SysParamterUtil.class);
    @Autowired
    TokenService tokenService;
    @Autowired
    private SysParameterMapper parameterMapper;

    Map<String, Object> parmaterMap = new HashMap<>();
    private final ReentrantLock lock = new ReentrantLock();

    /**
     * 加载
     */
    public void loadParamter() {
        List<Parameter> list = tokenService.query();
        if (list != null && list.size() > 0) {
            for (Parameter p : list) {
                parmaterMap.put(p.getKey(), p.getValue());
            }
        }
    }

    /**
     * 刷新
     */
    public void refreshParamter() {
        parmaterMap.clear();
        loadParamter();
    }

    /**
     * 取值
     *
     * @param key
     * @return
     */
    public Object getParametValue(String key) {
        if (parmaterMap.containsKey(key)) {
            return parmaterMap.get(key);
        }
        return null;
    }

    /**
     * 取值
     *
     * @param key
     * @param flag 追加
     * @return
     */
    public Object getParametValue(String key, boolean flag) {
        if (parmaterMap.containsKey(key)) {
            if (lock.tryLock()) {
                try {
                    if (flag && key.equals(Constants.ENV_ORDER)) {
                        String no = parmaterMap.get(key).toString();
                        no = no.replace(Constants.ENV_PREFIX, "");
                        Long num = Long.valueOf(no);
                        num++;
                        return Constants.ENV_PREFIX + num;
                    }
                    return parmaterMap.get(key);
                } catch (Exception e) {
                    log.error("获取参数异常", e);
                } finally {
                    lock.unlock();
                }
            }
        }
        return null;
    }

    /**
     * 设置值
     *
     * @param key
     * @param value
     */
    public void setParametValue(String key, Object value) {
        Parameter parameter = parameterMapper.selectOne(key);
        if (null != parameter) {
            if (lock.tryLock()) {
                try {
                    // 获取当前日期
                    LocalDate currentDate = LocalDate.now();
                    // 提取月份（1-12）
                    int month = currentDate.getMonthValue();
                    // 提取日份
                    int day = currentDate.getDayOfMonth();
                    if (month == 1 && day == 1) {
                        value = DateUtils.format(new Date(), "yyyyMMdd") + "00001";
                    }
                    parameter.setValue(value.toString());
                    parameterMapper.update(parameter);
                    parmaterMap.put(key, value);
                } catch (Exception e) {
                    log.error("设置参数异常", e);
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    public static void main(String[] args) {
        Long l = Long.valueOf("20251820811");
        l++;
        System.out.println(Constants.ENV_PREFIX + +l);
    }
}
